import allure
import pytest
from API_wms.utils.read import *
from API_wms.fixture.fixture import *

class Test_A3mall:

    # @pytest.mark.parametrize("data",read_excel(r'C:\Users\34098\PycharmProjects\Auto_wms\API_wms\data\canshu.xlsx'))
    # @pytest.mark.parametrize('data', read_csv(r"C:\Users\34098\PycharmProjects\Auto_wms\API_wms\data\canshu.CSV"))
    # @pytest.mark.parametrize("data",read_yaml(r'C:\Users\34098\PycharmProjects\Auto_wms\API_wms\data\login.yaml'))
    # @pytest.mark.parametrize('data',read_json(r"C:\Users\34098\PycharmProjects\Auto_wms\API_wms\data\login.json"))
    def test_login(self,api,jdbc,excel):
        # data = {"username":data["username"],"password":data["password"]}
        data = excel.read()
        print(data)
        data_login = {"username":data[0]['body_keys1'],"password":data[0]['body_keys2']}
        req = api.post(url=data[0]['address'],json=data_login)

        allure.attach(
            str(req.json()),
            name="断言",
            attachment_type=allure.attachment_type.JSON
        )

        with allure.step("响应状态码为200"):
            assert req.status_code == 200

        print(req.json())
        assert req.elapsed.total_seconds() <= 2
        assert data[0]['exp'] in req.json()["info"]
        if data[0]['exp'] == "ok":
            amount = jdbc.select("select amount from mall_users where username = '15712990423'")
            assert str(amount[0][0]) == req.json()['data']['amount']
            excel.write(4,6,"auth-token")
            excel.write(4,7,f"Bearer {req.json()['data']['token']}")
            excel.write(5, 6, "auth-token")
            excel.write(5, 7, f"Bearer {req.json()['data']['token']}")
        else:
            print('反向用例运行成功')

    def test_regiment(self,api,excel):
        regiment = api.get('/api/regiment?page=1&type=0&sort=1')
        assert regiment.json()["info"] == "ok"
        assert regiment.status_code == 200
        assert regiment.elapsed.total_seconds() <= 2
        print(regiment.json())
        excel.write(4,9,regiment.json()["data"]["list"][0]["id"])

    def test_buy(self,api,excel):
        data={
            "id": excel.read()[2]['body_keys1'],
            "type": "regiment",
            "address_id": 23,
            "store_id": 1,
            "shipping_type": 1,
            "bonus_id": "0",
            "payment": "balance",
            "remarks": "",
            "source": 1,
            "url": "http://10.59.9.12:7001/wap/pages/cart/confirm?id=2&sku_id=&num=1&type=regiment",
            "sku_id": "",
            "num": "1"
        }
        headers = {excel.read()[2]['headers']:excel.read()[2]['headers_keys']}
        create = api.post(url='/api/order/create',json=data,headers=headers)
        print(create.json())
        assert create.json()["data"]["msg"] == "支付成功"
        assert create.elapsed.total_seconds() <= 2
        assert create.status_code == 200
        excel.write(5,9,create.json()['data']['order_id'])

    def test_confirm(self,api,excel):
        data = {"id": excel.read()[3]['body_keys1']}
        headers = {excel.read()[3]['headers']:excel.read()[3]['headers_keys']}
        confirm = api.post(url='/api/order/confirm_delivery',json=data,headers=headers)
        assert confirm.json()["info"] == "确认收货成功"
        assert confirm.status_code == 200
        print(confirm.json())






